home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 19 / CU Amiga Magazine's Super CD-ROM 19 (1998)(EMAP Images)(GB)[!][issue 1998-02].iso / CUCD / Programming / LEDA / incl / LEDA.020+881 / d_array.h < prev    next >
C/C++ Source or Header  |  1994-08-05  |  5KB  |  159 lines

  1. /*******************************************************************************
  2. +
  3. +  LEDA  3.1c
  4. +
  5. +
  6. +  d_array.h
  7. +
  8. +
  9. +  Copyright (c) 1994  by  Max-Planck-Institut fuer Informatik
  10. +  Im Stadtwald, 6600 Saarbruecken, FRG     
  11. +  All rights reserved.
  12. *******************************************************************************/
  13.  
  14.  
  15. #ifndef LEDA_D_ARRAY_H
  16. #define LEDA_D_ARRAY_H
  17.  
  18. //------------------------------------------------------------------------------
  19. // d_array 
  20. //------------------------------------------------------------------------------ 
  21. #include <LEDA/impl/skiplist.h> 
  22. #define DA_DEF_IMPL skiplist
  23. typedef skiplist_item d_array_item;
  24.  
  25.  
  26. template<class itype, class etype> 
  27.  
  28. class _CLASSTYPE d_array : public virtual DA_DEF_IMPL 
  29. {
  30.  
  31. etype init;
  32. d_array_item iterator;
  33.  
  34. int  cmp(GenPtr x, GenPtr y) const
  35.                           { return compare(ACCESS(itype,x),ACCESS(itype,y)); }
  36. void clear_key(GenPtr& x)   const { Clear(ACCESS(itype,x)); }
  37. void clear_inf(GenPtr& x)   const { Clear(ACCESS(etype,x)); }
  38. void copy_key(GenPtr& x)    const { x=Copy(ACCESS(itype,x));  }
  39. void copy_inf(GenPtr& x)    const { x=Copy(ACCESS(etype,x));  }
  40. int  int_type()             const { return INT_TYPE(itype); }
  41.  
  42. public:
  43.  
  44. virtual etype&  operator[](itype y) 
  45. { d_array_item i=DA_DEF_IMPL::lookup(Convert(y));
  46.   if (i==nil) i=DA_DEF_IMPL::insert(Convert(y),Convert(init));
  47.   return ACCESS(etype,info(i)); 
  48. }
  49.  
  50. virtual bool defined(itype y)  const 
  51. { return (DA_DEF_IMPL::lookup(Convert(y))!=nil); }
  52.  
  53. virtual void start_iteration() const 
  54. { (d_array_item&)iterator = DA_DEF_IMPL::first_item(); }
  55.  
  56. virtual bool next_index(itype& y) const    
  57. { if (iterator==0) return false;
  58.   else { y = ACCESS(itype,key(iterator));
  59.          (d_array_item&)iterator = DA_DEF_IMPL::next_item(iterator);
  60.          return true; 
  61.         } 
  62. }
  63.  
  64. d_array<itype,etype>& operator=(const d_array<itype,etype>& A)
  65. { DA_DEF_IMPL::operator=(A); init=A.init; return *this; }
  66.  
  67.  
  68. d_array()        { Init(init); }
  69. d_array(etype i) { init=i; }
  70.  
  71. d_array(const d_array<itype,etype>& A) : DA_DEF_IMPL(A) {init=A.init;}
  72.  
  73. virtual ~d_array() { clear(); }
  74.  
  75. };
  76.  
  77. #define forall_defined(i,A)  for ((A).start_iteration(); (A).next_index(i); )
  78.  
  79.  
  80.  
  81. //------------------------------------------------------------------------------
  82. //
  83. // Dictionary arrays with implementation parameter:
  84. //
  85. //   _d_array<itype,etype,impl> 
  86. //
  87. //------------------------------------------------------------------------------
  88.  
  89.  
  90. #define _d_array_class(itype,etype,impl)\
  91. \
  92. class _CLASSTYPE _d_array_class_(itype,etype,impl):private virtual impl,\
  93.                                                    public d_array<itype,etype>\
  94. {\
  95. \
  96. etype init; /* because of a bug in g++ I use "this->init" for accessing it*/\
  97. d_array_item iterator;\
  98. \
  99. int cmp(GenPtr x, GenPtr y) const\
  100. { return compare(ACCESS(itype,x),ACCESS(itype,y)); }\
  101. void clear_key(GenPtr& x) const { Clear(ACCESS(itype,x)); }\
  102. void clear_inf(GenPtr& x) const { Clear(ACCESS(etype,x)); }\
  103. void copy_key(GenPtr& x) const { x=Copy(ACCESS(itype,x)); }\
  104. void copy_inf(GenPtr& x) const { x=Copy(ACCESS(etype,x)); }\
  105. void print_key(GenPtr x) const { Print(ACCESS(itype,x),cout); }\
  106. void print_inf(GenPtr x) const { Print(ACCESS(etype,x),cout); }\
  107. \
  108. int int_type() const { return INT_TYPE(itype); }\
  109. \
  110. public:\
  111. \
  112. virtual etype& operator[](itype y)\
  113. { d_array_item i=(d_array_item)impl::lookup(Convert(y));\
  114.   if (i==nil) i=(d_array_item)impl::insert(Convert(y),Convert(this->init));\
  115.   return ACCESS(etype,impl::info(impl::item(i)));\
  116. }\
  117. \
  118. virtual bool defined(itype y) const\
  119. { return (impl::lookup(Convert(y))!=nil); }\
  120. \
  121. virtual void start_iteration() const\
  122. { (d_array_item&)iterator = (d_array_item)impl::first_item(); }\
  123. \
  124. virtual bool next_index(itype& y) const    \
  125. { if (iterator==0) return false;\
  126.   else\
  127.   {y = ACCESS(itype,impl::key(impl::item(iterator)));\
  128.    (d_array_item&)iterator=(d_array_item)impl::next_item(impl::item(iterator));\
  129.    return true;\
  130.   }\
  131. }\
  132. \
  133. _d_array_(itype,etype,impl)& operator=(const _d_array_(itype,etype,impl)& A)\
  134. { impl::operator=(A); this->init=A.init; return *this; }\
  135. \
  136. _d_array_class_(itype,etype,impl)()        { Init(this->init); }\
  137. _d_array_class_(itype,etype,impl)(etype i) { this->init=i; }\
  138. \
  139. _d_array_class_(itype,etype,impl)(const _d_array_(itype,etype,impl)& A) : impl(A)\
  140. { this->init=A.init; }\
  141. \
  142. virtual ~_d_array_class_(itype,etype,impl)() { impl::clear(); }
  143.  
  144. #if defined(__TEMPLATE_ARGS_AS_BASE__)
  145. #define _d_array_class_(a,b,c) _d_array
  146. #define _d_array_(a,b,c) _d_array<a,b,c>
  147. template <class itype, class etype, class impl> 
  148. _d_array_class(itype,etype,impl)
  149. };
  150. #else
  151. #define _d_array(a,b,c)         name4(a,b,c,_d_array)
  152. #define _d_array_class_(a,b,c)  name4(a,b,c,_d_array)
  153. #define _d_array_(a,b,c)        name4(a,b,c,_d_array)
  154. #define _d_arraydeclare3(_a,_b,_c) _d_array_class(_a,_b,_c) };
  155. #endif
  156.  
  157. #endif
  158.